<?php
/************************FoxPHP威客系统************************/
/*                                                            */
/*  FoxPHP.Com (C)2006-2016 FoxPHP Inc.                       */
/*  This is NOT a freeware, use is subject to license terms   */
/*  FoxPHP威客系统是商业软件,使用于商业用途请购买授权         */
/*  V3.0 2016.3                                               */
/*  foxphp@aliyun.com                                         */
/*  http://www.foxphp.com                                     */
/*                                                            */
/**************************************************************/

namespace Admin\Model;
use Think\Model;

class CategoryModel extends AdminModel {
	
	//数据过滤-----------------------------------------------------------------------------------------------
	function FormatData($manage=array()){

		$data['catid']=I('get.catid',0,'int');
		$data['upid']=I('post.upid',0,'int');
		$data['name']=I('post.name');
		$data['route']=I('post.route');
		$data['icon']=I('post.icon');
		$data['task']=I('post.task',0,'int');
		$data['service']=I('post.service',0,'int');
		$data['news']=I('post.news',0,'int');
		$data['help']=I('post.help',0,'int');
		$data['taskfee']=I('post.taskfee',0,'int');
		$data['displayorder']=I('post.displayorder',0,'int');
		$data['title']=I('post.title');
		$data['keywords']=I('post.keywords');
		$data['description']=I('post.description');
		$data['SecurityForm']=I('post.SecurityForm');
		
		if(!empty($_FILES['file']['size'])){
			$upload=new \Upload\Model\FileModel();
			$file=$upload->StartUpload(1);
			if(!empty($file['filepath'])){
				$data['logo']=$file['filepath'];
				$fid=$file['id'];
				$data['logoid']=$fid;
				if(!empty($manage['logoid'])){
					$upload->DeleteUpload($manage['logoid']);
				}
				$upload->token(false)->where(" fid=$fid ")->save(array('isuse'=>1));
			}else{
				$this->error=$upload->getError();
			}
		}
		
		return $data;
	}

	//foxphp_json_category
	function UpdateJsonCategory($type=0){
		
		//全部
		$where=' upid=0 ';
		$url=FOXPHP_PATH.'/assets/js/foxphp_json_category.js';
		
		$outer='[';
		$withinone='';
		if($type==1){
			//任务
			$where.=' AND task=1 ';
			$url=FOXPHP_PATH.'/assets/js/foxphp_json_task_category.js';
		}elseif($type==2){
			//服务
			$where.=' AND service=1 ';
			$url=FOXPHP_PATH.'/assets/js/foxphp_json_fuwu_category.js';
		}elseif($type==3){
			//新闻
			$where.=' AND news=1 ';
			$url=FOXPHP_PATH.'/assets/js/foxphp_json_news_category.js';
		}elseif($type==4){
			//帮助
			$where.=' AND help=1 ';
			$url=FOXPHP_PATH.'/assets/js/foxphp_json_help_category.js';
		}

		$onearray=$this->where($where)->token(false)->order('displayorder ASC')->select();
		if(!empty($onearray)){
			foreach($onearray as $key=>$value){
				$withinnew='';

				if($withinone){
					$withinnew=',';
				}
				
				//二级
				$withintwo='';
				
				$upidone=$value['catid'];
				$twoarray=$this->where(" upid=$upidone ")->token(false)->order('displayorder ASC')->select();

				if(!empty($twoarray)){
					foreach($twoarray as $two){
						$withintwonew='';

						if($withintwo){
							$withintwonew=',';
						}
						
						//三级
						$withinthree='';
					
						$upidtwo=$two['catid'];
						$threearray=$this->where(" upid=$upidtwo ")->token(false)->order('displayorder ASC')->select();
						
						if(!empty($threearray)){
							foreach($threearray as $three){
								$withinthreenew='';
								if($withinthree){
									$withinthreenew=',';
								}
								
								$withinthree.=$withinthreenew.'{"name":"'.$three['name'].'","route":"'.$three['route'].'","id":'.$three['catid'].'}';
							}
						}
					
						$withintwo.=$withintwonew.'{"name":"'.$two['name'].'","route":"'.$two['route'].'","id":'.$two['catid'].',"subclass":['.$withinthree.']}';
						
					}
				}
				
				//一级
				$withinone.=$withinnew.'{"name":"'.$value['name'].'","route":"'.$value['route'].'","icon":"'.$value['icon'].'","id":'.$value['catid'].',"subclass":['.$withintwo.']}';
			}
		}
		
		
		$outer.=$withinone.']';
		
		WriteFile($url,$outer);
		
	}
}

?>